﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyExtension
{
    /// <summary>
    /// 
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// 判断字符串是否为null;null返回true,其他返回fals
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNull(this string value)
        {
            return value == null;
        }

        /// <summary>
        /// 判断字符串是否为null;null返回false,其他返回true
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNotNull(this string value)
        {
            return !value.IsNull();
        }

        /// <summary>
        /// 判断字符串是否为null;empty返回true,其他返回false
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }

        /// <summary>
        /// 判断字符串是否为null;empty返回false,其他返回true
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNotEmpty(this string value)
        {
            return !value.IsEmpty();
        }


        /// <summary>
        /// 字符串转换为Int32
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int? ConvertToInt32(this string value)
        {
            if (Int32.TryParse(value, out int result))
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 字符串转换为Int32
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static long? ConvertToInt64(this string value)
        {
            if (Int64.TryParse(value, out long result))
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 字符串转换为DateTime
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DateTime? ConvertToDateTime(this string value)
        {
            if (DateTime.TryParse(value, out DateTime result))
            {
                return result;
            }
            return null;
        }

    }
}
